package org.smarthings.things.management.impl;

import org.smarthings.things.management.Thing;
import org.smarthings.things.management.ThingActionCollection;
import org.smarthings.things.management.ThingPropertyCollection;

public class ThingImpl implements Thing {

	private String UUID;
	private String name;
	private String type;
	private int state;

	private String[] tags;

	private ThingPropertyCollectionImpl properties;
	private ThingActionCollectionImpl actions;
	private ThingsEventPropagator thingsEventPropagator;

	public ThingImpl(String name, String type, String[] tags,
			ThingPropertyCollectionImpl properties, ThingActionCollectionImpl actions,
			ThingsEventPropagator thingsEventPropagator) {
		this.name = name;
		this.type = type;
		this.tags = tags;
		this.UUID = generateThingUUID();
		this.thingsEventPropagator = thingsEventPropagator;
		
		this.properties = properties;
		if (this.properties != null) {
			this.properties.bindThing(this);
		}
		this.actions = actions;
		if (this.actions != null) {
			this.actions.bindThing(this);
		}

	}

	@Override
	public int getState() {
		return state;
	}

	@Override
	public String getUUID() {
		return UUID;
	}

	@Override
	public String getName() {
		return name;
	}

	@Override
	public String getType() {
		return type;
	}

	@Override
	public String[] getTags() {
		return tags;
	}

	@Override
	public void setTags(String[] tags) {
		this.tags = tags;
	}

	@Override
	public void setState(int state) {
		this.state = state;
		this.thingsEventPropagator.fireThingStateChangedEvent(this);
	}

	@Override
	public void setName(String name) {
		this.name = name;
	}

	@Override
	public void setType(String type) {
		this.type = type;
	}

	@Override
	public ThingPropertyCollection getProperties() {
		return properties;
	}

	@Override
	public ThingActionCollection getActions() {
		return actions;
	}

	private static long index = 0;

	private static String generateThingUUID() {
		return String.valueOf(++index); // FIXME
		// return java.lang.UUID.randomUUID().toString();
	}

}
